package baseuser

import (
	"context"
	"errors"
	"fmt"

	"usercenter/internal/svc"
	"usercenter/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type ChangeCreditLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewChangeCreditLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ChangeCreditLogic {
	return &ChangeCreditLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *ChangeCreditLogic) ChangeCredit(req *types.ChangeCreditRequest) (resp *types.ChangeCreditResponse, err error) {
    // 参数校验
    if req.UserID <= 0 {
        return nil, errors.New("用户ID必须大于0")
    }
    if req.Credit <= 0 {
        return nil, errors.New("积分值必须大于0")
    }

    // 根据操作类型调用不同的方法
    switch req.Operation {
    case "add":
        err = l.svcCtx.UserModel.AddCredits(req.UserID, req.Credit)
    case "reduce":
        err = l.svcCtx.UserModel.ReduceCredits(req.UserID, req.Credit)
    default:
        return nil, errors.New("不支持的操作类型")
    }

    // 处理错误
    if err != nil {
        logx.Errorf("修改积分失败: %v", err)
        return nil, fmt.Errorf("修改积分失败: %v", err)
    }

    // 返回成功响应
    return &types.ChangeCreditResponse{
        Code: 200,
        Message: "积分修改成功",
    }, nil
}